package cn.edu.baiyunu.myapplication.ui.screens.home.components

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.edu.baiyunu.myapplication.ui.components.GlassmorphismCard
import java.text.NumberFormat
import java.util.Locale

/**
 * 余额卡片组件，显示月度余额信息
 */
@Composable
fun BalanceCard(
    monthlyBalance: Double,
    monthlyIncome: Double,
    monthlyExpense: Double,
    modifier: Modifier = Modifier
) {
    GlassmorphismCard(
        modifier = modifier.fillMaxWidth()
    ) {
        Column(
            modifier = Modifier.padding(25.dp)
        ) {
            // 标题
            Text(
                text = "本月结余",
                fontSize = 16.sp,
                color = Color.White.copy(alpha = 0.8f),
                modifier = Modifier.padding(bottom = 20.dp)
            )
            
            // 余额金额
            Text(
                text = formatCurrency(monthlyBalance),
                fontSize = 32.sp,
                fontWeight = FontWeight.Bold,
                color = Color.White,
                modifier = Modifier.padding(bottom = 25.dp)
            )
            
            // 收入和支出详情
            Row(
                modifier = Modifier.fillMaxWidth()
            ) {
                // 收入部分
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.weight(1f)
                ) {
                    Text(
                        text = "收入",
                        fontSize = 14.sp,
                        color = Color.White.copy(alpha = 0.7f),
                        modifier = Modifier.padding(bottom = 5.dp)
                    )
                    Text(
                        text = formatCurrency(monthlyIncome),
                        fontWeight = FontWeight.Bold,
                        color = Color(0xFF26DE81) // 绿色
                    )
                }
                
                // 支出部分
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.weight(1f)
                ) {
                    Text(
                        text = "支出",
                        fontSize = 14.sp,
                        color = Color.White.copy(alpha = 0.7f),
                        modifier = Modifier.padding(bottom = 5.dp)
                    )
                    Text(
                        text = formatCurrency(monthlyExpense),
                        fontWeight = FontWeight.Bold,
                        color = Color(0xFFFF5E62) // 红色
                    )
                }
            }
        }
    }
}

/**
 * 格式化金额为货币格式
 */
private fun formatCurrency(amount: Double): String {
    val format = NumberFormat.getCurrencyInstance(Locale.CHINA)
    return format.format(amount).replace("¥", "￥")
} 